package cn.catsdad.page;

import cn.catsdad.base.page.PageParam;
import cn.catsdad.constant.iConstant.IDateFormat;
import cn.catsdad.util.DateUtil;
import cn.catsdad.util.StringUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能项查询参数
 *
 * @author xuyonglong
 * @version V1.0 2023/4/23
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class MenuParam extends PageParam {

    private Map<String, Object> paramMap = new HashMap<>();

    /**
     * 关键字查询，名称、权限编码、路由地址
     */
    private String key;

    /**
     * 状态检索，枚举MenuStatus值
     */
    private int status;

    /**
     * 创建时间开始时间查询
     */
    private String startTime;

    /**
     * 创建时间结束时间查询
     */
    private String endTime;

    @Override
    public Map<String, Object> getParamMap() {
        return this.paramMap;
    }

    @Override
    public String getCondition() {
        StringBuilder condition = new StringBuilder(" and m.isDeleted = 0");
        if (StringUtil.isNotEmpty(this.key)) {
            condition.append(" and (m.name like :key or m.permCode like :key or m.url like :key) ");
            this.paramMap.put("key", likeQuery(this.key));
        }
        if (this.status != 0) {
            condition.append(" and m.status = :status");
            this.paramMap.put("status", this.status);
        }
        if (StringUtil.isNotEmpty(this.startTime)) {
            Date date = DateUtil.parseDateFromStr(this.startTime, IDateFormat.YYYY_MM_DD);
            condition.append(" and m.creationTime >= :start");
            this.paramMap.put("start", date);
        }
        if (StringUtil.isNotEmpty(this.endTime)) {
            Date date = DateUtil.parseDateFromStr(this.endTime, IDateFormat.YYYY_MM_DD);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            condition.append(" and m.creationTime <= :end");
            this.paramMap.put("end", calendar.getTime());
        }
        return condition.toString();
    }
}
